package kaler.radiovirsa.app.service;

import static kaler.radiovirsa.app.constant.GlobalConfig.radiolinks_stream_sikh;
import static kaler.radiovirsa.app.constant.GlobalConfig.radiolinks_stream_virsa;
import kaler.radiovirsa.app.MainActivity;
import kaler.radiovirsa.app.constant.GlobalConfig;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class ManageRadio extends Service {
	MediaPlayer mp;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mp = new MediaPlayer();
		if (!mp.isPlaying()) {
			startRadio();
		}
	}

	void startRadio() {
		try {
			mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer arg0) {
					MainActivity.updateview();
					mp.start();
				}
			});

			if (GlobalConfig.channel == 1)
				mp.setDataSource(radiolinks_stream_virsa);
			else if (GlobalConfig.channel == 2)
				mp.setDataSource(radiolinks_stream_sikh);
			mp.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			MainActivity.updateview();
			mp.stop();
			mp.reset();
			mp.release();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

}
